using System;
using Microsoft.SPOT;
using Komodex.NETMF.MicroTweet;

namespace DCDev.Attendant.Actions
{
    class TwitterAction : IAttendantAction
    {
        private const string _consumerKey = "";
        private const string _consumerSecret = "";
        private const string _userToken = "";
        private const string _userTokenSecret = "";
        private const int _delayInSeconds = 15;

        private DateTime _lastNotification = DateTime.MinValue;
        private TwitterClient _client;

        /// <summary>
        /// Initializes a new instance of the TwitterAction class.
        /// </summary>
        /// <param name="client"></param>
        public TwitterAction()
        {
            _client = new TwitterClient(_consumerKey, _consumerSecret, _userToken, _userTokenSecret);
            _client.DebugMessage += new DebugMessageEventHandler(client_DebugMessage);
        }

        void client_DebugMessage(object sender, DebugMessageEventArgs e)
        {
            Debug.Print(e.Message);
        }

        #region IAttendantAction Members
        public bool Notify(bool isOpen)
        {
            if (_lastNotification.AddSeconds(_delayInSeconds) < DateTime.Now)
            {
                Debug.Print("Notifying via Twitter");

                if (_client.SendTweet((isOpen ? "Available" : "Occupied") + " as of " + DateTime.Now.AddHours(-4).ToString()))
                {
                    _lastNotification = DateTime.Now;
                    return true;

                }
            }

            return false;           
        }

        #endregion
    }
}
